package com.tang.filter;

import com.tang.entity.Admin;
import com.tang.entity.Reader;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author tang
 * @create 2020-08-19
 */
@WebFilter(value = "/reader")
public class ReaderFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpSession session = ((HttpServletRequest)servletRequest).getSession();
        Reader reader = (Reader) session.getAttribute("reader");
        Admin admin = (Admin) session.getAttribute("admin");
        if (reader == null&&admin ==null) {
            ((HttpServletResponse)servletResponse).sendRedirect("login.jsp");
        }
        else {
            filterChain.doFilter(servletRequest,servletResponse);
        }

    }
}
